/**
 * Created by lenovo on 2017/1/10.
 */


(function (module) {
    module.TimeUtils = {
        MONTH_IN_FIGURES: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        MONTH_IN_WORDS: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        WEEK_SHORT: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        WEEK: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        WEEK_SHORT_1: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
        WEEK_1: ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
        convertToDate: function (str) {
            str = str.replace(/-/g, '/');
            return new Date(str);
        },
        convertToLong: function (str) {
            return this.convertToDate(str).getTime();
        },
        format:function (date,fmt) {
            if(typeof(date)== 'undefined' || (typeof (date) =='string' && date=='')){
                return '';
            }
            if(typeof(date)==='number'){
                if(date <=0){
                    return '';
                }
                date = new Date(date);
            }
            var o = {
                'M+' : date.getMonth()+1, //月份
                'd+' : date.getDate(), //日
                'h+' : date.getHours()%12 == 0 ? 12 : date.getHours()%12, //小时
                'H+' : date.getHours(), //小时
                'm+' : date.getMinutes(), //分
                's+' : date.getSeconds(), //秒
                'q+' : Math.floor((date.getMonth()+3)/3), //季度
                'S' : date.getMilliseconds() //毫秒
            };
            var week = {
                '0' : '/u65e5',
                '1' : '/u4e00',
                '2' : '/u4e8c',
                '3' : '/u4e09',
                '4' : '/u56db',
                '5' : '/u4e94',
                '6' : '/u516d'
            };
            if(/(y+)/.test(fmt)){
                fmt=fmt.replace(RegExp.$1, (date.getFullYear()+'').substr(4 - RegExp.$1.length));
            }
            if(/(E+)/.test(fmt)){
                fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? '/u661f/u671f' : '/u5468') : '')+week[date.getDay()+'']);
            }
            for(var k in o){
                if(new RegExp('('+ k +')').test(fmt)){
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (('00'+ o[k]).substr((''+ o[k]).length)));
                }
            }
            return fmt;
        },
        timeline:function(input,now){
            now = now || new Date().getTime();
            var timeSpan = now - input;
            var time = 0;
            if (timeSpan < 60000) {
                return '刚刚';
            } else if (timeSpan < 3600000) {
                time = Math.floor( timeSpan / 60000);
                return time+'分钟前';
            } else if (timeSpan < 24 * 3600000) {
                time =  Math.floor(timeSpan / 3600000);
                return time+'小时前';
            } else if (timeSpan < 7 * 24 * 3600000) {
                time =  Math.floor(timeSpan / 24 / 3600000);
                return time+'天前';
            } else if (timeSpan < 30 * 24 * 3600000) {
                time  = Math.floor(timeSpan / 7 / 24 / 3600000);
                return time+'星期前';
            } else if (timeSpan < 365 * 24 * 3600000) {
                time  = Math.floor(timeSpan / 30 / 24 / 3600000);
                return time+'个月前';
            } else {
                return this.format(input,'YYYY年MM月DD日');
            }
        }
    };
})(window);